package com.hlingsoft.kkh.action.v20;

import com.hlingsoft.kkh.action.BaseAction;
import com.hlingsoft.kkh.dao.*;
import com.hlingsoft.kkh.entity.*;
import com.hlingsoft.kkh.entity.vo.EventsVo1;
import com.hlingsoft.kkh.entity.vo.StartsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;

import java.util.*;

/**
 * Created by selbylei on 2015/6/15.
 */
@Controller
@RequestMapping("v20/likes")
public class likesAction extends BaseAction {


    private static final int rows=10;


    /**
     * 点赞接口
     * @param userId
     * @param eventCommentsId
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> like(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "eventCommentsId") Integer eventCommentsId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Date now = new Date();
            if (userId==0){
                resultMap.put("msg","登陆之后才能点赞");
                resultMap.put("result",false);
            }else {
                Likes likes = new Likes();
                likes.setMyId(userId);
                likes.setCreateTime(now);
                likes.setUpdateTime(now);

                EventComments eventComments =eventCommentsDao.getCommentsListById(eventCommentsId);
                likes.setEventsId(eventComments.getEventId());
                likes.setAppUserId(eventComments.getAppUserId());
                likes.setEventCommentsId(eventCommentsId);
                likes.setAvailable(1);
                likesDao.addLikes(likes);

                Notification notification = new Notification();
                notification.setEventsId(eventComments.getEventId());
                notification.setMyId(userId);
                AppUser myUser = userDao.getUserById(userId);
                notification.setMyName(myUser.getName());
                notification.setMyImageHead(myUser.getHeadImage());
                AppUser toUser = userDao.getUserById(eventComments.getAppUserId());
                notification.setToId(toUser.getId());
                notification.setToName(toUser.getName());
                //跟帖的第一张图片
                List<Image> imageList = imageDao.getImageByEventCommentsId(eventCommentsId);
                notification.setToImage(imageList.get(0).getPath());
                notification.setContent("我给你点赞啦");
                notification.setType(4);
                notification.setTarget(2);
                notification.setCreateTime(now);
                notification.setUpdateTime(now);
                notificationDao.addNotification(notification);

                MessageStatus messageStatus = new MessageStatus();
                messageStatus.setAppUserId(eventComments.getAppUserId());
                messageStatus.setType(2);
                messageStatus.setMessageId(notification.getId());
                messageStatusDao.addMessageStatus(messageStatus);


                //返回首页用到的数据
                EventsVo1 eventsVo1 = new EventsVo1();
                Events events = eventsDao.getEventsById(eventComments.getEventId());
                List<AppUser> likesUserList = new ArrayList<AppUser>();
                //活动id
                eventsVo1.setId(events.getId());
                //活动名
                eventsVo1.setName(events.getName());
                eventsVo1.setImageList(imageList);
                //活动发布人信息
                eventsVo1.setAppUserId(eventComments.getAppUserId());
                eventsVo1.setAppUser(userDao.getUserById(eventComments.getAppUserId()));
                //活动描述
                eventsVo1.setDescription(eventComments.getContent());

                //评论
                List<Reply> replyList = replyDao.getReplyByEventCommentsId(eventCommentsId, 0, 2);
                Collections.sort(replyList, new Comparator<Reply>() {
                    @Override
                    public int compare(Reply o1, Reply o2) {
                        return o1.getCreateTime().compareTo(o2.getCreateTime());
                    }
                });
                eventsVo1.setReplyList(replyList);

                //经纬度
                eventsVo1.setLatitude(events.getLatitude());
                eventsVo1.setLongitude(events.getLongitude());
                //对点跟帖的评论id
                eventsVo1.setEventCommentsId(eventComments.getId());

                //创建时间
                eventsVo1.setCreateAddress(events.getCreateAddress());
                eventsVo1.setCreateTime(events.getCreateTime());
                eventsVo1.setUpdateTime(events.getUpdateTime());

                //点赞人
                List<Likes> likesList = likesDao.getLikesByEventCommentsId(eventComments.getId(), 0, 10);
                for (Likes likes1 : likesList) {
                    AppUser appUser = userDao.getUserById(likes1.getMyId());
                    if (appUser != null) {
                        likesUserList.add(appUser);
                    }
                }
                eventsVo1.setLikesUserList(likesUserList);
                eventsVo1.setLikesCount(likesDao.getLikesByEventCommentsIdCount(eventComments.getId()));

                //是否能点赞
                if (userId != 0) {
                    if (likesDao.getCIsLikes(userId, eventComments.getId()).size() == 0) {
                        eventsVo1.setIsLike(0);
                    } else {
                        eventsVo1.setIsLike(1);
                    }
                } else {
                    eventsVo1.setIsLike(1);
                }
                resultMap.put("eventsVo1",eventsVo1);

                resultMap.put("likes","likes");
                resultMap.put("result",true);
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 活动点赞人列表
     * @param userId
     * @param eventCommentId
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.json")
    @ResponseBody
    public Map<String, Object> edit(
            @RequestParam(value = "userId",required = false) Integer userId,
            @RequestParam(value = "eventCommentId") long eventCommentId,
            @RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<StartsVo> startsVoList = new ArrayList<StartsVo>();
            List<Likes> likesList = likesDao.getLikesByEventCommentsId(eventCommentId, (pageNum - 1) * rows, rows);
            for (Likes likes:likesList){
                StartsVo startsVo = new StartsVo();
                //获取点赞人
                AppUser appUser = userDao.getUserById(likes.getMyId());
                if (appUser!=null){
                    //获取点赞人粉丝数
                    long fansCount = focusDao.getFansByIdCount(appUser.getId());
                    appUser.setFansCount(fansCount);
                    //判断是否可以关注
                    if (userId!=null){
                        if (focusDao.getFocus(userId,appUser.getId()) == null) {
                            appUser.setIsFocus(0);
                        } else {
                            appUser.setIsFocus(1);
                            if (focusDao.getFocus(appUser.getId(),userId)!=null){
                                appUser.setIsFocus(2);
                            }
                        }
                    }else {
                        appUser.setIsFocus(0);
                    }

                    startsVo.setAppUser(appUser);
                    Events events= eventsDao.getNewestEventsByUserId(appUser.getId());
                    startsVo.setNewestEvents(events);

                    startsVoList.add(startsVo);
                }

            }
            resultMap.put("startsVoList",startsVoList);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

}
